package com.asion.vmovie.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.adapters.SearchAdapter;
import com.asion.vmovie.adapters.SearchListAdapter;
import com.asion.vmovie.app.MyApp;
import com.asion.vmovie.beans.DetialsBean;
import com.asion.vmovie.db.search;
import com.asion.vmovie.db.searchDao;
import com.asion.vmovie.ui.MVPContract.SearchContract;
import com.asion.vmovie.ui.MVPModel.SearchModel;
import com.asion.vmovie.ui.MVPPresenter.SearchPresenter;
import com.asion.vmovie.utils.ToastUtil;

import org.greenrobot.greendao.query.Query;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by asion on 16-10-20.
 */

public class SearchActivity extends BaseActivity<SearchPresenter, SearchModel> implements SearchContract.View, TextView.OnEditorActionListener, AdapterView.OnItemClickListener {

    @BindView(R.id.search_content)
    EditText searchContent;
    @BindView(R.id.search_cancle)
    Button searchCancle;
    @BindView(R.id.search_db)
    ListView searchDb;
    @BindView(R.id.search_input_del)
    ImageView searchInputDel;
    @BindView(R.id.search_recycler)
    RecyclerView searchRecycler;
    private List<String> stringList = new ArrayList<>();
    private SearchAdapter searchAdapter;

    public static void showAction(Activity activity) {
        Intent intent = new Intent(activity, SearchActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }


    @Override
    public int getLayoutId() {
        return R.layout.search_activity;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        List<search> list = MyApp.getSearchDao().queryBuilder().build().list();
        for (search search : list) {
            stringList.add(search.getContent());
        }
        searchCancle.setOnClickListener(v -> finish());
        searchInputDel.setOnClickListener(v -> searchContent.setText(null));

        searchContent.setOnEditorActionListener(this);

        if (stringList.size() != 0) {
            searchDb.setVisibility(View.VISIBLE);
            View view = LayoutInflater.from(this).inflate(android.R.layout.simple_list_item_1, null);
            TextView textView = (TextView) view.findViewById(android.R.id.text1);
            textView.setText("清空历史记录");
            textView.setOnClickListener(v -> {
                MyApp.getSearchDao().deleteAll();
                searchDb.setVisibility(View.GONE);
            });
            searchDb.setAdapter(new SearchListAdapter(stringList, this));
            searchDb.addFooterView(view);
            searchDb.setOnItemClickListener(this);
        } else {
            searchDb.setVisibility(View.GONE);
        }

        searchAdapter = new SearchAdapter(this);
        searchRecycler.setItemAnimator(new DefaultItemAnimator());
        searchRecycler.setLayoutManager(new LinearLayoutManager(this));
        searchRecycler.setAdapter(searchAdapter);

        searchAdapter.setOnItemClickListener(dataBean -> {
            Bundle bundle = new Bundle();
            bundle.putString("postId",dataBean.getPostid());
            bundle.putString("url",dataBean.getRequest_url());
            bundle.putString("likecount",dataBean.getLike_num());
            bundle.putString("sharecount",dataBean.getShare_num());
            bundle.putString("commentcount",dataBean.getDuration());
            WebActivity.showAction(this,bundle);
        });


    }

    private boolean loading = false;
    private int page =1;

    @Override
    public void showDataSuccess(DetialsBean detialsBean) {
        searchAdapter.removeAll();
        searchAdapter.addAll(detialsBean.getData());
        loading = false;
    }

    @Override
    public void showDataError(Throwable throwable) {
        ToastUtil.showShort("网络请求失败");
        loading = false;
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            String s = String.valueOf(v.getText());
            new Thread() {
                @Override
                public void run() {
                    search search = new search(null, s);
                    Query<com.asion.vmovie.db.search> query = MyApp.getSearchDao().queryBuilder().where(searchDao.Properties.Content.eq(s)).build();
                    if (query.list().size() == 0) {
                        MyApp.getSearchDao().insertOrReplace(search);
                    }
                }
            }.start();
            searchDb.setVisibility(View.GONE);

            mPresenter.getSearchData(1,20,s);

            return true;
        }
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s = stringList.get(position);
        searchContent.setText(s);
    }
}
